<template>
  <el-select
    v-model="selectValue"
    class="m-2"
    placeholder="Select"
    size="large"
  >
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value"
    />
  </el-select>
  <el-button @click="addControl">添加控件 </el-button>

  <el-form :model="Controls" label-width="120px">
    <div v-for="(control, index) in Controls" :key="index">
      <component
        :is="control.controlType"
        :Control="control"
      ></component>
    </div>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">Create</el-button>
      <el-button>Cancel</el-button>
      <span>
        {{ Controls }}
      </span>
    </el-form-item>
  </el-form>
</template>
  <script lang="ts" setup>
import { ref, reactive, getCurrentInstance } from "vue";
import nipponinput from "./nipponinput.vue";
import nipponradio from "./nipponradio.vue";
import type { Control } from "../../components/base/type";

const current = getCurrentInstance();
if (current) {
  current.appContext.components = {
    ...current.appContext.components,
    nipponinput,
    nipponradio,
  };
}
const Controls = reactive<Control[]>([]);
const selectValue = ref("");
const options = ref([
  {
    label: "文本框",
    value: "nipponinput",
  },
  {
    label: "单选框",
    value: "nipponradio",
  },
]);
Controls.push(
  {
    controlKey: "1",
    controlType: "nipponinput",
    controlValue: "nipponinput_1",
  },
  {
    controlKey: "2",
    controlType: "nipponinput",
    controlValue: "nipponinput_2",
  },
  {
    controlKey: "3",
    controlType: "nipponinput",
    controlValue: "nipponinput_3",
  },
  {
    controlKey: "4",
    controlType: "nipponradio",
    controlValue: "1",
    controlKeyNames: [
      {
        label: "苹果",
        value: "1",
      },
      {
        label: "香蕉",
        value: "2",
      },
    ],
  }
);

// function changeSize(e : any) {
//   alert(e);
//   defaultSize.value = e;
// }

function onSubmit() {
  console.log(Controls);
}

function addControl() {
  const len = Controls.length;

  if (selectValue.value == "nipponradio") {
    Controls.push({
      controlKey: (len + 1).toString(),
      controlType: selectValue.value,
      controlValue: "",
      controlKeyNames: [
        {
          label: "苹果",
          value: "1",
        },
        {
          label: "香蕉",
          value: "2",
        },
      ],
    });
  } else {
    Controls.push({
      controlKey: (len + 1).toString(),
      controlType: selectValue.value,
      controlValue: "",
    });
  }
}
</script>